package pshop.servlet;

import pshop.Bestellung;
import pshop.db.OfyService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AdminServlet extends HttpServlet
{

   private static final long serialVersionUID = 5236547253465453L;

   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
   {
      work(req, resp);
   }

   @Override
   public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
   {
      work(req, resp);
   }

   public void work(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
   {
      List<Bestellung> result = OfyService.ofy().load().type(Bestellung.class).list();
      List<Bestellung> bests = new ArrayList<Bestellung>();
      for(Bestellung best : result){
        bests.add(best);
      }
      req.getSession().setAttribute("bests", bests);
      req.getRequestDispatcher("/admin/bestellungen.jsp").forward(req, resp);
   }

}